package io.gbznt.modules.buss.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.gbznt.modules.buss.entity.BussRtuInfoEntity;
import io.gbznt.modules.buss.entity.BussRtuSendRecordEntity;
import io.gbznt.modules.buss.service.BussRtuInfoService;
import io.gbznt.modules.buss.service.BussRtuSendRecordService;
import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.R;
import io.gbznt.common.utils.ShiroUtils;

/**
 * RTU下行报文数据
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-05-20 12:08:14
 */
@RestController
@RequestMapping("buss/bussrtusendrecord")
public class BussRtuSendRecordController {
	@Autowired
	private BussRtuSendRecordService bussRtuSendRecordService;
	@Autowired
	private BussRtuInfoService bussRtuInfoService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("buss:bussrtusendrecord:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = bussRtuSendRecordService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("buss:bussrtusendrecord:info")
	public R info(@PathVariable("id") String id) {
		BussRtuSendRecordEntity bussRtuSendRecord = bussRtuSendRecordService.getById(id);

		return R.ok().put("bussRtuSendRecord", bussRtuSendRecord);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("buss:bussrtusendrecord:save")
	public R save(@RequestBody BussRtuSendRecordEntity bussRtuSendRecord) {
		// 查询该田块下的rtu信息
		QueryWrapper<BussRtuInfoEntity> rtuqw = new QueryWrapper<>();
		rtuqw.eq("farmland_info_id", bussRtuSendRecord.getFarmlandInfoId());
		List<BussRtuInfoEntity> rtuList = bussRtuInfoService.list(rtuqw);
		for (BussRtuInfoEntity bussRtuInfoEntity : rtuList) {
			BussRtuSendRecordEntity xxbw = new BussRtuSendRecordEntity();
			xxbw.setGateA(bussRtuSendRecord.getGateA());// 闸门1
			xxbw.setGateB(bussRtuSendRecord.getGateB());// 闸门2
			xxbw.setGateC(bussRtuSendRecord.getGateC());// 闸门3
			xxbw.setGateD(bussRtuSendRecord.getGateD());// 闸门4
			xxbw.setGateE(bussRtuSendRecord.getGateE());// 闸门5
			// xxbw.setZhanghao("00 12 34 56 78");// 遥测站号
			// xxbw.setZhongxinghao("01");// 中心站号
			xxbw.setCreateId(ShiroUtils.getUserId());// 创建人
			xxbw.setCreateTime(new Date());// 创建时间
			xxbw.setStatus(0);// 报文状态
			xxbw.setFarmlandInfoId(bussRtuInfoEntity.getFarmlandInfoId());// 田块
			xxbw.setRtuId(bussRtuInfoEntity.getId());// rtu
			xxbw.setDeptId(bussRtuInfoEntity.getDeptId());// 基地
			xxbw.setPassword(bussRtuInfoEntity.getPassword());// rtu密码
			xxbw.setBaowentype("4E");// 功能码（报文特征）

			// 5个闸门用于拼接报文
			String zhamen1 = "00", zhamen2 = "00", zhamen3 = "00", zhamen4 = "00", zhamen5 = "00";
			if (xxbw.getGateA().equals("1")) {
				zhamen1 = "50";
			}
			if (xxbw.getGateB().equals("1")) {
				zhamen2 = "50";
			}
			if (xxbw.getGateC().equals("1")) {
				zhamen3 = "50";
			}
			if (xxbw.getGateD().equals("1")) {
				zhamen4 = "50";
			}
			if (xxbw.getGateE().equals("1")) {
				zhamen5 = "50";
			}

			// rtu序列号，用于塞入报文
			String rtuNum = bussRtuInfoEntity.getSerialNum();
			String[] str = new String[5];
			str[0] = rtuNum.substring(0, 2);
			str[1] = rtuNum.substring(2, 4);
			str[2] = rtuNum.substring(4, 6);
			str[3] = rtuNum.substring(6, 8);
			str[4] = rtuNum.substring(8, 10);

			// 发报时间 15 10 16 14 58 38
			Date date = new Date();
			String dateStr = new SimpleDateFormat("yy MM dd HH mm ss").format(date);
			xxbw.setFabaoshijian(dateStr);// 发报时间
			// 下行报文格式
			// 7E 7E 00 12 34 56 78 01 12 34 4E 80 0D 02 00 00 15 10 16 14
			// 58 38 01 00 00 20 00 30 03 00 00 10 04 00 00 80 05 00 00 60
			// 05 EF CF
			String bw = "7E 7E " + str[0] + " " + str[1] + " " + str[2] + " " + str[3] + " " + str[4]
					+ " 01 12 34 4E 80 0D 02 00 00 " + dateStr + " 01 00 00 " + zhamen1 + " 02 00 00 " + zhamen2
					+ " 03 00 00 " + zhamen3 + " 04 00 00 " + zhamen4 + " 05 00 00 " + zhamen5 + " 05 EF CF";
			// 报文
			xxbw.setMessageCode(bw);

			bussRtuSendRecordService.save(xxbw);// 新增下行报文
		}
		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("buss:bussrtusendrecord:update")
	public R update(@RequestBody BussRtuSendRecordEntity bussRtuSendRecord) {
		bussRtuSendRecordService.updateById(bussRtuSendRecord);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("buss:bussrtusendrecord:delete")
	public R delete(@RequestBody String[] ids) {
		bussRtuSendRecordService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

	/**
	 * 根据田块id获取最后一条下行报文
	 */
	@RequestMapping("/getByTkId")
	public R getByTkId(@RequestParam Map<String, Object> params) {
		List<BussRtuSendRecordEntity> list = bussRtuSendRecordService.getByTkId(params);
		if (list.size() > 0) {
			return R.ok().put("xxbw", list.get(0));
		} else {
			return R.ok();
		}

	}

	/**
	 * 根据定时报间隔，日起始时间，rtuId添加下行报文
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping("/save1")
	@RequiresPermissions("buss:bussrtusendrecord:save")
	public R save1(@RequestParam Map<String, Object> params) {
		// 获取rtuId
		String rtuId = (String) params.get("rtuId");
		// 定时报间隔
		String dsbjg = (String) params.get("dsbjg");
		// 日起始时间
		String rqqssj = (String) params.get("rqqssj");
		// 根据rtuId查询出rtu信息
		BussRtuInfoEntity rtu = bussRtuInfoService.getById(rtuId);
		// 初始化下行报文
		BussRtuSendRecordEntity xxbw = new BussRtuSendRecordEntity();

		xxbw.setDsbjg(dsbjg);// 定时报间隔
		xxbw.setRqqssj(rqqssj);// 日起始时间
		xxbw.setSwjz("1m");// 水位基值
		xxbw.setSwsx("1m");// 水位上限
		xxbw.setSwxx("1m");// 水位下限
		xxbw.setZhanghao("00 12 34 56 78");// 遥测站号
		xxbw.setZhongxinghao("01");// 中心站号

		xxbw.setCreateId(ShiroUtils.getUserId());// 创建人
		xxbw.setCreateTime(new Date());// 创建时间
		xxbw.setStatus(0);// 报文状态
		xxbw.setFarmlandInfoId(rtu.getFarmlandInfoId());// 田块
		xxbw.setRtuId(rtu.getId());// rtu
		xxbw.setDeptId(rtu.getDeptId());// 基地
		xxbw.setPassword(rtu.getPassword());// rtu密码
		xxbw.setBaowentype("42");// 功能码（报文特征）
		// 下行报文格式
		// 7E 7E 00 12 34 56 78 01 12 34 42 00 3F 02 00 1F 18 01 30 14
		// 20 34 F1 F1 00 12 34 56 78 20 08 01 21 08 07 22 08 00 20 23
		// 08 00 80 24 08 00 30 1B 19 4F

		// rtu序列号，用于塞入报文
		String rtuNum = rtu.getSerialNum();
		String[] str = new String[5];
		str[0] = rtuNum.substring(0, 2);
		str[1] = rtuNum.substring(2, 4);
		str[2] = rtuNum.substring(4, 6);
		str[3] = rtuNum.substring(6, 8);
		str[4] = rtuNum.substring(8, 10);

		// 发报时间 15 10 16 14 58 38
		Date date = new Date();
		String dateStr = new SimpleDateFormat("yy MM dd HH mm ss").format(date);
		xxbw.setFabaoshijian(dateStr);// 发报时间
		String bw = "7E 7E " + str[0] + " " + str[1] + " " + str[2] + " " + str[3] + " " + str[4]
				+ " 01 12 34  00 3F 02 00 1F " + dateStr + " F1 F1 00 12 34 56 78 20 08 "
				+ xxbw.getDsbjg().split(":")[0] + " 21 08 " + xxbw.getRqqssj().split(":")[0]
				+ " 22 08 00 20 23 08 00 80 24 08 00 30 1B 19 4F";
		// 报文
		xxbw.setMessageCode(bw);

		bussRtuSendRecordService.save(xxbw);// 新增下行报文

		return R.ok();
	}

}
